package licode.unisop.client.vo;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 定义平台认证后返回的值
 *
 * @author WR
 * @since 2016/6/14 17:36
 */
@Data
@Builder
@AllArgsConstructor
public class SopAuthPlatform {
    // 定义configs里的key值
    public static String FILTER_URL = "filterUrl";

    /**
     * 平台认证后生成的凭证
     */
    private String token;

    /**
     * 内部模块之间访问的凭证
     */
    private String modelToken;

    /**
     * 内部凭证
     */
    private String innerToken;

    /**
     * 平台名称
     */
    private String name;

    /**
     * 平台所属领域ID
     */
    private String realmId;

    /**
     * 平台标识
     */
    private String platformId;

    /**
     * 平台票据地址
     */
    private String ticketUrl;

    private List<SopUrl> urls;
    private Map<String, Object> configs;

    public SopAuthPlatform() {
        modelToken = "";
        token = "";
        innerToken = "";
        name = "";
        platformId = "";
        ticketUrl = "";
        configs = new HashMap<>();
    }

    public boolean isValid() {
        return token != null && !token.isEmpty();
    }

    public boolean configAsBoolean(String key, boolean def) {
        if (!configs.containsKey(key)) {
            return def;
        }
        return "true".equals(configs.get(key).toString());
    }

    public SopAuthPlatform clonePl() {
        SopAuthPlatform clonePl = new SopAuthPlatform();

        clonePl.modelToken = this.modelToken;
        clonePl.token = this.token;
        clonePl.innerToken = this.innerToken;
        clonePl.name = this.name;
        clonePl.platformId = this.platformId;
        clonePl.ticketUrl = this.ticketUrl;
        if (null != this.urls) {
            clonePl.urls = new ArrayList<>(this.urls);
        }
        clonePl.configs = new HashMap<>(this.configs);
        clonePl.realmId = this.realmId;

        return clonePl;
    }
}
